package health.lm.com.shop.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.SearchView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XListActivity;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.HLPeopleAdapter;
import health.lm.com.shop.entity.HLPeopleEntity;
import health.lm.com.shop.presenter.CompanyMemberListPresenter;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.activity
 * Date：2019/8/24 14:29
 * Introduce：会员列表页面
 */
@Route(path = ARouterConstant.CompanyMemberListActivity)
public class CompanyMemberListActivity extends XListActivity<CompanyMemberListPresenter> {
    @Autowired
    public String id;
    @BindView(R2.id.titlebar)
    CommonTitleBar mTitlebar;
    @BindView(R2.id.searchView)
    SearchView mSearchView;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.srl_layout)
    SmartRefreshLayout mSrlLayout;
    private HLPeopleAdapter mAdapter;
    private String keyword = "";

    /**
     * 加载数据
     *
     * @param isRefresh true 刷新 false 加载更多
     * @param layout    头部布局
     * @param page      页数
     * @param pageSize  每页数量
     */
    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        getP().getData(id, page, pageSize, keyword);
    }

    @Override
    public int getLayoutId() {
        return R.layout.hl_activity_people;
    }

    @Override
    public CompanyMemberListPresenter newP() {
        return new CompanyMemberListPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitlebar.setListener((v, action, extra) -> finish());
        mTitlebar.getCenterTextView().setText("商会会员");
        mSearchView.onActionViewExpanded();
        mSearchView.setQueryHint("请输入你要查找的姓名");
        mAdapter = new HLPeopleAdapter(new ArrayList<>(), 1);
        mRvList.setAdapter(mAdapter);
        super.recyclerView = mRvList;
        super.rlRefreshLayout = mSrlLayout;
        super.adapter = mAdapter;
        super.initData(savedInstanceState);
        mRvList.setLayoutManager(new LinearLayoutManager(context));
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                isRefresh = true;
                page = 1;
                keyword = newText;
                getP().getData(id, page, pageSize, newText);
                return false;
            }
        });
        getP().getData(id, page, pageSize, keyword);

        mAdapter.setOnItemClickListener((adapter1, view, position) -> {
            HLPeopleEntity entity = (HLPeopleEntity) adapter1.getData().get(position);
            ARouter.getInstance().build(ARouterConstant.HLPeopleDetailActivity)
                    .withString("id", entity.getId() + "")
                    .withString("name", entity.getName())
                    .navigation();
        });
    }

    public void setNewData(List<HLPeopleEntity> pNewData) {
        if (isRefresh) {
            mSrlLayout.finishRefresh();
            mAdapter.setNewData(pNewData);
            initLoadMore();
        } else {
            mAdapter.addData(pNewData);
            if (pNewData.size() >= pageSize) mAdapter.loadMoreComplete();
            else mAdapter.loadMoreEnd();
        }
        if (mAdapter.getData().size() <= 0) mAdapter.setEmptyView(empty());
    }
}
